package main

import (
	"log"
	"runtime"
)

func main() {
	// demo1()
	demo2()
}

// 使用无缓冲的chan 等待goroutine
func demo1() {
	ch := make(chan struct{})

	go func(c chan struct{}) {
		num := 0
		for i := 0; i < 1000; i++ {
			num += 1
		}
		log.Println("num=", num)
		c <- struct{}{}
	}(ch)

	log.Println("最大线程：", runtime.NumGoroutine())
	// 读取通道
	<-ch
}

// 使用无缓冲的chan 等待goroutine
func demo2() {
	ch := make(chan struct{})
	ci := make(chan int, 100)
	go func(c chan<- struct{}, c1 chan<- int) {
		num := 0
		for i := 0; i < 50; i++ {
			num += 1
			c1 <- num
		}

		close(c1)

		log.Println("num=", num)
		c <- struct{}{}
	}(ch, ci)

	log.Println("最大线程：", runtime.NumGoroutine())
	// 读取通道
	<-ch

	for v := range ci {
		log.Println(v)
	}
}
